import {
  fetchCampainSalerList, fetchCampainSalerLinkList
} from '@/services/campain/CampainCommon';

export default {
  namespace: 'CampainCommon',

  state: {
    salerList: [],
    salerLinkList: [],
  },

  effects: {
    * fetchCampainSalerList({payload}, {call, put}) {
      const response = yield call(fetchCampainSalerList, payload);
      if (response.code === 0) {
        yield put({
          type: 'chgSalerList',
          payload: response.data.map(d => {
            d.key = d.id;
            d.value = d.id;
            d.label = d.name;
            return d;
          }),
        });
      }
    },

    * fetchCampainSalerLinkList({payload}, {call, put}) {
      const response = yield call(fetchCampainSalerLinkList, payload);
      if (response.code === 0) {
        yield put({
          type: 'chgSalerLinkList',
          payload: response.data.map(d => {
            d.key = d.campainSaler.id;
            d.value = d.campainSaler.id;
            d.label = d.campainSaler.name;
            return d;
          }),
        });
      }
    },

  },

  reducers: {
    chgSalerList(state, action) {
      return {
        ...state,
        salerList: action.payload,
      };
    },

    chgSalerLinkList(state, action) {
      return {
        ...state,
        salerLinkList: action.payload,
      };
    },

  },

};
